package com.example.testapplication.ui.main;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;

import com.example.testapplication.BR;
import com.example.testapplication.R;
import com.example.testapplication.databinding.ActivityMainBinding;
import com.example.testapplication.ui.home.HomeFragment;
import com.example.testapplication.ui.mine.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import me.goldze.mvvmhabit.base.BaseActivity;

public class MainActivity extends BaseActivity<ActivityMainBinding,MainViewModel> {
    private List<Fragment> mFragments;
    private boolean isLight;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        initFragment();
        initBottomTab();
    }

    @Override
    protected boolean isStatusBarDarkFont() {
        return false;
    }

    @Override
    public void initViewObservable() {
        viewModel.uc.pRxBusEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                Log.e("QQ","Main:RxBus|——>"+aBoolean);
                isLight = aBoolean;
            }
        });
    }

    private void initBottomTab() {
        binding.tab.setItemHorizontalTranslationEnabled(true);
        binding.tab.setLabelVisibilityMode(NavigationBarView.LABEL_VISIBILITY_LABELED);
        binding.tab.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.i_home:
                        commitAllowingStateLoss(0);
                        ImmersionBar.with(MainActivity.this).statusBarDarkFont(isLight).init();
                        break;
                    case R.id.i_user:
                        commitAllowingStateLoss(1);
                        ImmersionBar.with(MainActivity.this).statusBarDarkFont(false).init();
                }
                return true;
            }
        });
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(new HomeFragment());
        mFragments.add(new MineFragment());
        //默认选中第一个
        commitAllowingStateLoss(0);
    }





    private void commitAllowingStateLoss(int position) {
        hideAllFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(position + "");
        if (currentFragment != null) {
            transaction.show(currentFragment);
        } else {
            currentFragment = mFragments.get(position);
            transaction.add(R.id.frag_container, currentFragment, position + "");
        }
        transaction.commitAllowingStateLoss();
    }

    //隐藏所有Fragment
    private void hideAllFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mFragments.size(); i++) {
            Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(i + "");
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
        }
        transaction.commitAllowingStateLoss();
    }
}